home *** CD-ROM | disk | FTP | other *** search
/ Openstep 4.2 (Developer) / Openstep Developer 4.2.iso / NextDeveloper / Source / GNU / uucp / Uucp.framework / uuconf.subproj / hinit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-09  |  7.9 KB  |  296 lines

  1. /* hinit.c
  2.    Initialize for reading HDB configuration files.
  3.  
  4.    Copyright (C) 1992, 1994 Ian Lance Taylor
  5.  
  6.    This file is part of the Taylor UUCP uuconf library.
  7.  
  8.    This library is free software; you can redistribute it and/or
  9.    modify it under the terms of the GNU Library General Public License
  10.    as published by the Free Software Foundation; either version 2 of
  11.    the License, or (at your option) any later version.
  12.  
  13.    This library is distributed in the hope that it will be useful, but
  14.    WITHOUT ANY WARRANTY; without even the implied warranty of
  15.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.    Library General Public License for more details.
  17.  
  18.    You should have received a copy of the GNU Library General Public
  19.    License along with this library; if not, write to the Free Software
  20.    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  21.  
  22.    The author of the program may be contacted at ian@airs.com or
  23.    c/o Cygnus Support, 48 Grove Street, Somerville, MA 02144.
  24.    */
  25.  
  26. #include "uucnfi.h"
  27.  
  28. #if USE_RCS_ID
  29. const char _uuconf_hinit_rcsid[] = "$Id: hinit.c,v 1.8 1995/06/21 19:22:51 ian Rel $";
  30. #endif
  31.  
  32. #include <errno.h>
  33. #include <ctype.h>
  34.  
  35. /* Avoid replicating OLDCONFIGLIB several times if not necessary.  */
  36. static const char abHoldconfiglib[] = OLDCONFIGLIB;
  37.  
  38. /* Initialize the routines which read HDB configuration files.  */
  39.  
  40. int
  41. uuconf_hdb_init (ppglobal, zprogram)
  42.      pointer *ppglobal;
  43.      const char *zprogram;
  44. {
  45.   struct sglobal **pqglobal = (struct sglobal **) ppglobal;
  46.   int iret;
  47.   struct sglobal *qglobal;
  48.   pointer pblock;
  49.   char abdialcodes[sizeof OLDCONFIGLIB + sizeof HDB_DIALCODES - 1];
  50.   char *zsys;
  51.   FILE *e;
  52.  
  53.   if (*pqglobal == NULL)
  54.     {
  55.       iret = _uuconf_iinit_global (pqglobal);
  56.       if (iret != UUCONF_SUCCESS)
  57.     return iret;
  58.     }
  59.  
  60.   qglobal = *pqglobal;
  61.   pblock = qglobal->pblock;
  62.  
  63.   if (zprogram == NULL
  64.       || strcmp (zprogram, "uucp") == 0)
  65.     zprogram = "uucico";
  66.  
  67.   /* Add the Dialcodes file to the global list.  */
  68.   memcpy ((pointer) abdialcodes, (pointer) abHoldconfiglib,
  69.       sizeof OLDCONFIGLIB - 1);
  70.   memcpy ((pointer) (abdialcodes + sizeof OLDCONFIGLIB - 1),
  71.       (pointer) HDB_DIALCODES, sizeof HDB_DIALCODES);
  72.   iret = _uuconf_iadd_string (qglobal, abdialcodes, TRUE, FALSE,
  73.                   &qglobal->qprocess->pzdialcodefiles,
  74.                   pblock);
  75.   if (iret != UUCONF_SUCCESS)
  76.     return iret;
  77.  
  78.   /* Read the Sysfiles file.  We allocate the name on the heap rather
  79.      than the stack so that we can return it in
  80.      qerr->uuconf_zfilename.  */
  81.  
  82.   zsys = uuconf_malloc (pblock,
  83.             sizeof OLDCONFIGLIB + sizeof HDB_SYSFILES - 1);
  84.   if (zsys == NULL)
  85.     {
  86.       qglobal->ierrno = errno;
  87.       return UUCONF_MALLOC_FAILED | UUCONF_ERROR_ERRNO;
  88.     }
  89.   memcpy ((pointer) zsys, (pointer) abHoldconfiglib, sizeof OLDCONFIGLIB - 1);
  90.   memcpy ((pointer) (zsys + sizeof OLDCONFIGLIB - 1), (pointer) HDB_SYSFILES,
  91.       sizeof HDB_SYSFILES);
  92.  
  93.   iret = UUCONF_SUCCESS;
  94.  
  95.   e = fopen (zsys, "r");
  96.   if (e == NULL)
  97.     uuconf_free (pblock, zsys);
  98.   else
  99.     {
  100.       char *zline;
  101.       size_t cline;
  102.       char **pzargs;
  103.       size_t cargs;
  104.       char **pzcolon;
  105.       size_t ccolon;
  106.       int cchars;
  107.  
  108.       zline = NULL;
  109.       cline = 0;
  110.       pzargs = NULL;
  111.       cargs = 0;
  112.       pzcolon = NULL;
  113.       ccolon = 0;
  114.  
  115.       qglobal->ilineno = 0;
  116.  
  117.       while (iret == UUCONF_SUCCESS
  118.          && (cchars = _uuconf_getline (qglobal, &zline, &cline, e)) > 0)
  119.     {
  120.       int ctypes, cnames;
  121.       int i;
  122.  
  123.       ++qglobal->ilineno;
  124.  
  125.       --cchars;
  126.       if (zline[cchars] == '\n')
  127.         zline[cchars] = '\0';
  128.       if (zline[0] == '#')
  129.         continue;
  130.  
  131.       ctypes = _uuconf_istrsplit (zline, '\0', &pzargs, &cargs);
  132.       if (ctypes < 0)
  133.         {
  134.           qglobal->ierrno = errno;
  135.           iret = UUCONF_MALLOC_FAILED | UUCONF_ERROR_ERRNO;
  136.           break;
  137.         }
  138.  
  139.       if (ctypes == 0)
  140.         continue;
  141.  
  142.       if (strncmp (pzargs[0], "service=", sizeof "service=" - 1) != 0)
  143.         {
  144.           iret = UUCONF_SYNTAX_ERROR;
  145.           break;
  146.         }
  147.       pzargs[0] += sizeof "service=" - 1;
  148.  
  149.       cnames = _uuconf_istrsplit (pzargs[0], ':', &pzcolon, &ccolon);
  150.       if (cnames < 0)
  151.         {
  152.           qglobal->ierrno = errno;
  153.           iret = UUCONF_MALLOC_FAILED | UUCONF_ERROR_ERRNO;
  154.           break;
  155.         }
  156.  
  157.       for (i = 0; i < cnames; i++)
  158.         if (strcmp (zprogram, pzcolon[i]) == 0)
  159.           break;
  160.  
  161.       if (i >= cnames)
  162.         continue;
  163.  
  164.       for (i = 1; i < ctypes && iret == UUCONF_SUCCESS; i++)
  165.         {
  166.           char ***ppz;
  167.           int cfiles, ifile;
  168.           
  169.           if (strncmp (pzargs[i], "systems=", sizeof "systems=" - 1)
  170.           == 0)
  171.         {
  172.           ppz = &qglobal->qprocess->pzhdb_systems;
  173.           pzargs[i] += sizeof "systems=" - 1;
  174.         }
  175.           else if (strncmp (pzargs[i], "devices=", sizeof "devices=" - 1)
  176.                == 0)
  177.         {
  178.           ppz = &qglobal->qprocess->pzhdb_devices;
  179.           pzargs[i] += sizeof "devices=" - 1;
  180.         }
  181.           else if (strncmp (pzargs[i], "dialers=", sizeof "dialers=" - 1)
  182.                == 0)
  183.         {
  184.           ppz = &qglobal->qprocess->pzhdb_dialers;
  185.           pzargs[i] += sizeof "dialers=" - 1;
  186.         }
  187.           else
  188.         {
  189.           iret = UUCONF_SYNTAX_ERROR;
  190.           break;
  191.         }
  192.  
  193.           cfiles = _uuconf_istrsplit (pzargs[i], ':', &pzcolon, &ccolon);
  194.           if (cfiles < 0)
  195.         {
  196.           qglobal->ierrno = errno;
  197.           iret = UUCONF_MALLOC_FAILED | UUCONF_ERROR_ERRNO;
  198.           break;
  199.         }
  200.  
  201.           for (ifile = 0;
  202.            ifile < cfiles && iret == UUCONF_SUCCESS;
  203.            ifile++)
  204.         {
  205.           /* Looking for a leading '/' is Unix dependent, and
  206.              should probably be changed.  */
  207.           if (pzcolon[ifile][0] == '/')
  208.             iret = _uuconf_iadd_string (qglobal, pzcolon[ifile], TRUE,
  209.                         FALSE, ppz, pblock);
  210.           else
  211.             {
  212.               char *zdir;
  213.               size_t clen;
  214.  
  215.               clen = strlen (pzcolon[ifile]);
  216.               zdir = (char *) uuconf_malloc (pblock,
  217.                              (sizeof OLDCONFIGLIB
  218.                               + sizeof HDB_SEPARATOR
  219.                               + clen
  220.                               - 1));
  221.               if (zdir == NULL)
  222.             {
  223.               qglobal->ierrno = errno;
  224.               iret = UUCONF_MALLOC_FAILED | UUCONF_ERROR_ERRNO;
  225.               break;
  226.             }
  227.               memcpy ((pointer) zdir, (pointer) abHoldconfiglib,
  228.                   sizeof OLDCONFIGLIB - 1);
  229.               memcpy ((pointer) (zdir + sizeof OLDCONFIGLIB - 1),
  230.                   HDB_SEPARATOR, sizeof HDB_SEPARATOR - 1);
  231.               memcpy ((pointer) (zdir
  232.                      + sizeof OLDCONFIGLIB - 1
  233.                      + sizeof HDB_SEPARATOR - 1),
  234.                   (pointer) pzcolon[ifile], clen + 1);
  235.               iret = _uuconf_iadd_string (qglobal, zdir, FALSE, FALSE,
  236.                           ppz, pblock);
  237.             }
  238.         }
  239.         }
  240.     }
  241.  
  242.       (void) fclose (e);
  243.       if (zline != NULL)
  244.     free ((pointer) zline);
  245.       if (pzargs != NULL)
  246.     free ((pointer) pzargs);
  247.       if (pzcolon != NULL)
  248.     free ((pointer) pzcolon);
  249.  
  250.       if (iret != UUCONF_SUCCESS)
  251.     {
  252.       qglobal->zfilename = zsys;
  253.       return iret | UUCONF_ERROR_FILENAME | UUCONF_ERROR_LINENO;
  254.     }
  255.     }
  256.  
  257.   if (qglobal->qprocess->pzhdb_systems == NULL)
  258.     {
  259.       char ab[sizeof OLDCONFIGLIB + sizeof HDB_SYSTEMS - 1];
  260.  
  261.       memcpy ((pointer) ab, (pointer) abHoldconfiglib,
  262.           sizeof OLDCONFIGLIB - 1);
  263.       memcpy ((pointer) (ab + sizeof OLDCONFIGLIB - 1),
  264.           (pointer) HDB_SYSTEMS, sizeof HDB_SYSTEMS);
  265.       iret = _uuconf_iadd_string (qglobal, ab, TRUE, FALSE,
  266.                   &qglobal->qprocess->pzhdb_systems,
  267.                   pblock);
  268.     }
  269.   if (qglobal->qprocess->pzhdb_devices == NULL && iret == UUCONF_SUCCESS)
  270.     {
  271.       char ab[sizeof OLDCONFIGLIB + sizeof HDB_DEVICES - 1];
  272.  
  273.       memcpy ((pointer) ab, (pointer) abHoldconfiglib,
  274.           sizeof OLDCONFIGLIB - 1);
  275.       memcpy ((pointer) (ab + sizeof OLDCONFIGLIB - 1),
  276.           (pointer) HDB_DEVICES, sizeof HDB_DEVICES);
  277.       iret = _uuconf_iadd_string (qglobal, ab, TRUE, FALSE,
  278.                   &qglobal->qprocess->pzhdb_devices,
  279.                   pblock);
  280.     }
  281.   if (qglobal->qprocess->pzhdb_dialers == NULL && iret == UUCONF_SUCCESS)
  282.     {
  283.       char ab[sizeof OLDCONFIGLIB + sizeof HDB_DIALERS - 1];
  284.  
  285.       memcpy ((pointer) ab, (pointer) abHoldconfiglib,
  286.           sizeof OLDCONFIGLIB - 1);
  287.       memcpy ((pointer) (ab + sizeof OLDCONFIGLIB - 1),
  288.           (pointer) HDB_DIALERS, sizeof HDB_DIALERS);
  289.       iret = _uuconf_iadd_string (qglobal, ab, TRUE, FALSE,
  290.                   &qglobal->qprocess->pzhdb_dialers,
  291.                   pblock);
  292.     }
  293.  
  294.   return iret;
  295. }
  296.